/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.nodes;

import com.googlecode.ivybeans.facade.ArtifactInfo;
import com.googlecode.ivybeans.facade.IvyLibrary;
import com.googlecode.ivybeans.facade.ResolvedIvyLibrary;
import com.googlecode.ivybeans.module.IvyDescriptorHelper;
import com.googlecode.ivybeans.module.IvyLibraryController;
import com.googlecode.ivybeans.module.classpath.ClassPathScope;
import com.googlecode.ivybeans.module.resources.ResourcesAccess;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.netbeans.api.project.Project;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;

/**
 * 
 * @author lforet
 */
public class LibrariesNode extends AbstractFolderNode {

    private final static String NODE_NAME = NbBundle.getMessage(ResourcesAccess.class, "CTL_LibrariesNode");
    private static final Image ICON_BADGE = Utilities.loadImage("org/netbeans/modules/java/j2seproject/ui/resources/libraries-badge.png");    //NOI18N
    static final RequestProcessor rp = new RequestProcessor();
    private Project project;

    public LibrariesNode(Project project) {
        super(new LibrariesChildren(project));
        this.project = project;
    }

    @Override
    public String getDisplayName() {
        return NODE_NAME;
    }

    @Override
    public String getName() {
        return NODE_NAME;
    }

    @Override
    protected Image getBadge() {
        return ICON_BADGE;
    }

    @Override
    public boolean canCopy() {
        return false;
    }

    @Override
    public Action[] getActions(boolean context) {
        return new Action[]{new AddLibraryAction(), new ForceResolveAction()};
    }

    private class AddLibraryAction extends AbstractAction {

        public AddLibraryAction() {
            putValue(Action.NAME, NbBundle.getMessage(ResourcesAccess.class, "BTN_Add_Library"));
        }

        public void actionPerformed(ActionEvent event) {
            IvyLibrary ivyLibrary = (IvyLibrary) project.getLookup().lookup(IvyLibrary.class);
            AddLibraryPanel pnl = new AddLibraryPanel(ivyLibrary);

            DialogDescriptor dd = new DialogDescriptor(pnl, NbBundle.getMessage(ResourcesAccess.class, "TIT_Add_Library"));
            dd.setClosingOptions(new Object[]{
                        pnl.getOkButton(),
                        DialogDescriptor.CANCEL_OPTION
                    });
            dd.setOptions(new Object[]{
                        pnl.getOkButton(),
                        DialogDescriptor.CANCEL_OPTION
                    });
            Object ret = DialogDisplayer.getDefault().notify(dd);

            if (pnl.getOkButton() == ret) {
                IvyLibraryController ilc = (IvyLibraryController) project.getLookup().lookup(IvyLibraryController.class);

                IvyDescriptorHelper.getInstance().addDependency(ilc.getDescriptorFO(),
                        pnl.getOrg(), pnl.getLibraryName(), pnl.getRevision(), pnl.getConf());
            }
        }
    }

    private class ForceResolveAction extends AbstractAction {

        public ForceResolveAction() {
            putValue(Action.NAME, NbBundle.getMessage(ResourcesAccess.class, "BTN_Force_Resolve"));
        }

        public void actionPerformed(ActionEvent event) {
            IvyLibraryController ilc = (IvyLibraryController) project.getLookup().lookup(IvyLibraryController.class);
            ilc.triggerIvyResolution();
        }
    }

    private static class LibrariesChildren extends Children.Keys<ClassPathScope>  {

        private Project project;
        
        public LibrariesChildren(Project project) {
            this.project = project;
            setKeys();
        }

        @Override
        protected Node[] createNodes(ClassPathScope key) {
            return new Node[]{new ScopeNode(project, key)};
        }

        
        private void setKeys() {
            setKeys(ClassPathScope.values());
        }
    }

}
